﻿using QWManage.DAL;
using QWManage.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace QWManage.Forms
{
    public partial class FrmGroupsOfCustomer : Form
    {
        public string ExuserID { get; set; }
        public FrmGroupsOfCustomer(string exuserid)
        {
            ExuserID = exuserid;
            InitializeComponent();
            lvGroups.MouseDoubleClick += LvGroups_MouseDoubleClick;
        }

        private void LvGroups_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvGroups.GetItemAt(e.X, e.Y);
            if (item == null) return;
            GroupInfo groupInfo = item.Tag as GroupInfo;
            FrmCustomersOfGroup frm = new FrmCustomersOfGroup(groupInfo.ChatID);
            frm.ShowDialog();
        }

        private void FrmGroupsOfCustomer_Load(object sender, EventArgs e)
        {
            menuStrip.Visible = false;
            toolStrip.Visible = false;
            lblStatus.Text = string.Empty;

            lvGroups.View = View.Details;
            lvGroups.MultiSelect = true;
            lvGroups.FullRowSelect = true;
            lvGroups.Columns.Add(new ColumnHeader() { Text = "群ID号", Width = 300 });
            lvGroups.Columns.Add(new ColumnHeader() { Text = "群名称", Width = 400 });
            lvGroups.Columns.Add(new ColumnHeader() { Text = "状态", Width = 100 });

            CustomerDAL customerDAL = new CustomerDAL();
            CustomerInfo customerInfo = customerDAL.GetCustomerInfo(ExuserID);
            if (customerInfo != null) Text = "群成员：" + customerInfo.Name + " [" + customerInfo.Exuserid + "]";

            CustomersGroupsDAL customersGroupsDAL = new CustomersGroupsDAL();
            List<CustomerGroupInfo> customerGroupInfos = customersGroupsDAL.GetCustomerGroupInfosByUserID(ExuserID);
            GroupDAL groupDAL = new GroupDAL();
            foreach (CustomerGroupInfo customerGroupInfo in customerGroupInfos)
            {
                GroupInfo groupInfo = groupDAL.GetGroupChatInfo(customerGroupInfo.GroupID);
                ListViewItem item = new ListViewItem(groupInfo.ChatID);
                item.Tag = groupInfo;
                item.SubItems.Add(groupInfo.Name);
                item.SubItems.Add(customerGroupInfo.Status ? "在群" : "不在群");
                if (!customerGroupInfo.Status)
                    item.ForeColor = Color.Red;
                lvGroups.Items.Add(item);
            }
        }
    }
}
